from utils.common import load_logging_config,dump_json
import os
import logging
import shutil
from consta import path_sep

def add_frontend_processor_and_feature_extractor():
    name="fpfe1"
    typ="fpfe"
    file="features/speech_features.py"
    if os.path.exists(file):
        if not os.path.abspath(file).startswith(os.path.abspath('fpfes')):
            target_fpfe_py=f'fpfes/{os.path.splitext(file.split("/")[-1])[0]}.py'
            if os.path.basename(target_fpfe_py) in os.listdir('fpfes'):
                logging.error('前端处理器和特征提取器模块重名')
            shutil.copy(file,target_fpfe_py)
            modulename=target_fpfe_py[:-3].replace('/','.')
        else:
            modulename=os.path.relpath(file,'.').replace(path_sep,'.')
            modulename=modulename if not modulename.endswith('.py') else modulename[:-3]
    elif file!='':
        modulename=file if not file.endswith('.py') else file[:-3]
        modulename=modulename.replace('/','.')
    else:
        logging.error('前端处理器和特征提取器模块名不能为空')

    classname='SpecAugment'
    parameters='sample_rate'
    attribute='run(sample_rate)'
    fpfe_json={
        'name':name,
        'type':typ,
        'modulename':modulename,
        'classname':classname,
        'parameters':parameters,
        'attribute':attribute
    }

    if not f'{fpfe_json["name"]}.json' in os.listdir('jsons/fpfes'):
        dump_json(fpfe_json,f'jsons/fpfes/{fpfe_json["name"]}.json')
    else:
        logging.error('前端处理器和特征提取器json文件重名，请重命名')

if __name__=='__main__':
    load_logging_config()
    add_frontend_processor_and_feature_extractor()